#pragma once
#include <iostream>
#include <vector>
using namespace std;

template<class T>
class Less
{
public:
	bool operator()(const T& x, const T& y)
	{
		return x < y;
	}
};

template<class T>
class Greater
{
public:
	bool operator()(const T& x, const T& y)
	{
		return x > y;
	}
};

template<class T, class Container = vector<T>, class Compare = Less<T>>
class Priority_queue
{
public:
	Priority_queue() = default;

	template<class InputIterator>
	Priority_queue(InputIterator first, InputIterator last)
		:_con(first, last)
	{
		for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
		{
			AdjustDown(i);
		}
	}

	const T& top()
	{
		return _con[0];
	}

	bool empty()
	{
		return _con.empty();
	}

	void push(const T& data)
	{
		_con.push_back(data);
		AdjustUp(_con.size() - 1);
	}

	void pop()
	{
		swap(_con[0], _con[_con.size() - 1]);
		_con.pop_back();
		AdjustDown(0);
	}

	void AdjustUp(size_t child)
	{
		size_t parent = (child - 1) / 2;
		while (child > 0)
		{
			if (_com(_con[parent], _con[child]))
			{
				swap(_con[parent], _con[child]);
				child = parent;
				parent = (child - 1) / 2;
			}
			else
			{
				break;
			}
		}
	}

	void AdjustDown(size_t parent)
	{
		size_t child = parent * 2 + 1;
		while (child < _con.size())
		{
			if (child + 1 < _con.size() && _com(_con[child], _con[child + 1]))
			{
				child++;
			}
			if (_com(_con[parent], _con[child]))
			{
				swap(_con[parent], _con[child]);
				parent = child;
				child = parent * 2 + 1;
			}
			else
			{
				break;
			}
		}
	}
private:
	Container _con;
	Compare _com;
};